package com.sunyard.utils;

import com.amazonaws.services.dynamodbv2.xspec.S;
import com.ibm.mq.*;
import com.ibm.mq.constants.MQConstants;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
import java.util.concurrent.CountDownLatch;

/**
 * @ClassName MqService
 * @Description TODO
 * @Author qinhao
 * Date 2021/4/13 9:26
 * Version 1.0
 **/

@Slf4j
public class MqService implements Runnable {

    private static MqService instance;

    private MQQueueManager recvManager;
    private MQQueueManager recvManager1;
    private MQQueueManager sendManager;
    private MQQueueManager sendManager1;
    private MQQueue sendQueue;
    private MQQueue sendQueue1;
    public static MQQueue recvQueue;
    public static MQQueue recvQueue1;
    @SuppressWarnings("rawtypes")
    private Hashtable props;


    private boolean alive = true; // 连接池活动状态
    private boolean recoveryFlag = true; // 消息读取失败后是否停止接收消息 (true:不停止，false:停止)

//    public MqService() {
//        try {
//            init();
//        } catch (Throwable e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                destroy();
//            } catch (MQException e) {
//                e.printStackTrace();
//            }
//        }
//
//    }

    public static MqService getInstance() {
        if (instance == null) {
            instance = new MqService();
        }
        return instance;
    }

    public void start() {
        log.info("Starting... " + this + ", receiveQueue:" + recvQueue);
        Thread t = new Thread(new Acceptor(0));
        t.start();
        log.info("Starting... " + this + ", receiveQueue:" + recvQueue1);
        Thread t1 = new Thread(new Acceptor(1));
        t1.start();
        log.info("Starting... " + this + ", receiveQueue:" + recvQueue);
        log.info("Starting... " + this + ", receiveQueue:" + recvQueue1);
    }

    public void restart() {
        shutdown();
        setAlive(true);
        start();
    }

    public void shutdown() {
        alive = false;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    private void init() throws Throwable {
        props = new Hashtable();
        props.put(MQConstants.HOST_NAME_PROPERTY, "66.5.60.133");
        props.put(MQConstants.PORT_PROPERTY, 10455);
        props.put(MQConstants.CCSID_PROPERTY, new Integer(1381));
        props.put(MQConstants.CHANNEL_PROPERTY, "SVRCONN_GW_ZHSD");
        props.put(MQConstants.TRANSPORT_PROPERTY, MQConstants.TRANSPORT_MQSERIES);

        if (recvManager == null) {
            recvManager = new MQQueueManager("FT_QMG2GW02", props);
        }
        if (sendManager == null) {
            sendManager = new MQQueueManager("FT_QMG2GW02", props);
        }
        int recvOpenOptions = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_FAIL_IF_QUIESCING;
        if (recvQueue == null) {
            recvQueue = recvManager.accessQueue("IBM.SERVICE.RESPONSE.OUT.ZHSD", recvOpenOptions, null, null, null);
        }
        int sendOpenOptions = MQConstants.MQOO_OUTPUT | MQConstants.MQOO_FAIL_IF_QUIESCING;
        if (sendQueue == null) {
            sendQueue = sendManager.accessQueue("IBM.SERVICE.REQUEST.IN", sendOpenOptions, null, null, null);
        }

        props = new Hashtable();
        props.put(MQConstants.HOST_NAME_PROPERTY, "66.5.60.133");
        props.put(MQConstants.PORT_PROPERTY, 10355);
        props.put(MQConstants.CCSID_PROPERTY, new Integer(1381));
        props.put(MQConstants.CHANNEL_PROPERTY, "SVRCONN_GW_ZHSD");
        props.put(MQConstants.TRANSPORT_PROPERTY, MQConstants.TRANSPORT_MQSERIES);
        if (recvManager1 == null) {
            recvManager1 = new MQQueueManager("FT_QMG2GW01", props);
        }
        if (sendManager1 == null) {
            sendManager1 = new MQQueueManager("FT_QMG2GW01", props);
        }
        int recvOpenOptions1 = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_FAIL_IF_QUIESCING;
        if (recvQueue1 == null) {
            recvQueue1 = recvManager1.accessQueue("IBM.SERVICE.REQUEST.OUT.ZHSD", recvOpenOptions1, null, null, null);
        }
        int sendOpenOptions1 = MQConstants.MQOO_OUTPUT | MQConstants.MQOO_FAIL_IF_QUIESCING;
        if (sendQueue1 == null) {
            sendQueue1 = sendManager1.accessQueue("IBM.SERVICE.REQUEST.OUT.ZHSD", sendOpenOptions1, null, null, null);
        }
    }

    /**
     * MQ 读取数据
     *
     * @return
     * @throws MQException
     * @throws IOException
     */
    private MQMessage read(int line) throws MQException, IOException {
        MQMessage recvMsg = new MQMessage();
        MQGetMessageOptions gmo = new MQGetMessageOptions();
        gmo.options = gmo.options | MQConstants.MQGMO_WAIT;
        gmo.waitInterval = MQConstants.MQWI_UNLIMITED;
        gmo.options = gmo.options + MQConstants.MQGMO_FAIL_IF_QUIESCING;
        try {
            if (line == 0) {
                recvQueue.get(recvMsg, gmo);
            } else {
                recvQueue1.get(recvMsg, gmo);
            }
        } catch (Exception ex) {
            try {
                end();
                init();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        return recvMsg;
    }

    /**
     * MQ 返回数据
     *
     * @param sendMsg
     * @throws MQException
     */
    public void write(MQMessage sendMsg, int line) throws MQException {
        MQPutMessageOptions pmo = new MQPutMessageOptions();
        if (line == 0) {
            sendQueue.put(sendMsg, pmo);
        } else {
            sendQueue1.put(sendMsg, pmo);
        }
    }

    /**
     * 返回信息处理
     *
     * @return
     * @throws IOException
     */
    public MQMessage buildMQMessage(MQMessage recvMsg) throws IOException {
        if (recvMsg != null) {
            log.info("MessageLength = " + recvMsg.getMessageLength());
        }
        byte[] msgContent = new byte[recvMsg.getMessageLength()];
        recvMsg.readFully(msgContent);
        String requestData = new String(msgContent, "UTF-8");
        log.info("接受的信息是:" + requestData);
        byte[] data = requestData.getBytes("utf-8");
        MQMessage sendMsg = new MQMessage();
        sendMsg.priority = 0;
        sendMsg.persistence = MQConstants.MQPER_NOT_PERSISTENT;
        sendMsg.expiry = 120000;
        sendMsg.format = MQConstants.MQFMT_STRING;
        sendMsg.messageId = recvMsg.messageId;
        sendMsg.correlationId = recvMsg.messageId;
        sendMsg.characterSet = 1208;
        if (StringUtils.isEmpty(data.toString())) {
            return sendMsg;
        }
        sendMsg.write(data);
        log.info("返回的信息是:" + new String(data));
        return sendMsg;
    }

    public void destroy() throws MQException {
        if (recvManager != null) {
            recvManager.close();
        }
        if (recvManager1 != null) {
            recvManager1.close();
        }
        if (sendManager != null) {
            sendManager.close();
        }
        if (sendManager1 != null) {
            sendManager1.close();
        }
    }

    @SneakyThrows
    @Override
    public void run() {
        String respXml = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n" +
                "<Service> \n" +
                "	<Service_Header>\n" +
                "		<reply_qmgr>FT_IN01</reply_qmgr>\n" +
                "		<processes>\n" +
                "			<process>\n" +
                "				<end_timestamp></end_timestamp>\n" +
                "				<process_timestamp>2021-04-16 16:30:36.578</process_timestamp>\n" +
                "				<resp_target_q>IBM.SERVICE.RESPONSE.OUT.ZHSD</resp_target_q>\n" +
                "				<req_target_q>IBM.SERVICE.REQUEST.OUT.ZHSD</req_target_q>\n" +
                "				<sub_target_id/>\n" +
                "				<target_id>2060</target_id>\n" +
                "				<sub_service_sn>9914561122029466713</sub_service_sn>\n" +
                "				<sub_reversal_service_id></sub_reversal_service_id>\n" +
                "				<async_reversal_service_id></async_reversal_service_id>\n" +
                "				<is_end>0</is_end>\n" +
                "				<skip_to_process>0</skip_to_process>\n" +
                "				<after_logic_class></after_logic_class>\n" +
                "				<timeout>150</timeout>\n" +
                "				<reversal_seq>0</reversal_seq>\n" +
                "				<key_service>0</key_service>\n" +
                "				<status>COMPLETE</status>\n" +
                "				<service_id>20600000000007</service_id>\n" +
                "				<code>404</code>\n" +
                "				<desc></desc>\n" +
                "			</process>\n" +
                "		</processes>\n" +
                "		<service_response></service_response>\n" +
                "		<msglog>1</msglog>\n" +
                "		<timeout>150</timeout>\n" +
                "		<resp_target_q>IBM.SERVICE.RESPONSE.OUT.IFP</resp_target_q>\n" +
                "		<req_target_q>IBM.SERVICE.REQUEST.OUT.IFP</req_target_q>\n" +
                "		<msg_expiry>360000</msg_expiry>\n" +
                "		<start_time>1618561836575</start_time>\n" +
                "		<start_timestamp>2021-04-16 16:30:36.575</start_timestamp>\n" +
                "		<name>订单查询</name>\n" +
                "		<service_sn>9914561122029466713</service_sn>\n" +
                "		<service_id>20600000000007</service_id>\n" +
                "		<requester_id>0536</requester_id>\n" +
                "		<branch_id>320099999</branch_id>\n" +
                "		<channel_id>03</channel_id>\n" +
                "		<version_id>01</version_id>\n" +
                "		<need_request>true</need_request>\n" +
                "		<service_time>20210416163317</service_time>\n" +
                "		<trace_msg>SERVICE.SINGLE.ROUTE - Sent single request to provider</trace_msg>\n" +
                "	</Service_Header>\n" +
                "	<Service_Body>\n" +
                "		<ext_attributes>\n" +
                "			<channelcode>004</channelcode>\n" +
                "			<channeldate>20210416</channeldate>\n" +
                "			<channelseq>145611053123</channelseq>\n" +
                "			<brno>320121001</brno>\n" +
                "			<templatecode>000014</templatecode>\n" +
                "			<sysid>000014</sysid>\n" +
                "			<terminal_unitno>3207210011A0010001</terminal_unitno>\n" +
                "			<terminal_name>高一一</terminal_name>\n" +
                "			<terminal_acctno>6224524711040987868</terminal_acctno>\n" +
                "			<INM-ENC-DEV-ID>01.wy.zpk</INM-ENC-DEV-ID>\n" +
                "			<plat_version>two_plat</plat_version>\n" +
                "		</ext_attributes>\n" +
                "		<addition/>\n" +
                "		<request>\n" +
                "			<outTradeNo>123456</outTradeNo>\n" +
                "			<mchntCd>3207210011A0010001</mchntCd>\n" +
                "		</request>\n" +
                "		<response>\n" +
                "			<transInfos>\n" +
                "				<transInfo>\n" +
                "					<outTradeNo>2021000038</outTradeNo>\n" +
                "					<transType>1</transType>\n" +
                "					<transStatus>0</transStatus>\n" +
                "					<transAmount>39</transAmount>\n" +
                "					<transDatetime>2021-04-16 16:59:59</transDatetime>\n" +
                "					<remark>微信交易</remark>\n" +
                "					<handlingCharge>7.8</handlingCharge>\n" +
                "					<shopName>江苏农商银行</shopName>\n" +
                "					<mchntName>信雅达</mchntName>\n" +
                "				</transInfo>\n" +
                "			</transInfos>\n" +
                "		</response>\n" +
                "	</Service_Body>\n" +
                "</Service>\n";
        log.info("开始接收请求消息");
        MQMessage msg = serviceRec();
        log.info("开始返回响应消息");
        System.out.printf(Thread.currentThread().getName());
        serviceSend(msg, respXml);
    }

    /**
     * 接受线程
     */
    public class Acceptor implements Runnable {
        private int line;

        public Acceptor() {

        }

        public Acceptor(int line) {
            this.line = line;
        }

        @Override
        public void run() {
            while (isAlive()) {
                try {
                    MQMessage msg = read(line);
                    // MqValue.pool.execute(new Writer(msg, line));
                } catch (Exception e) {
                    if (e instanceof MQException) {
                        log.error(((MQException) e).getMessage());
                    } else
                        log.error("Receive message error: ", e);
                    if (!recoveryFlag) {
                        break;
                    }
                }
            }
        }
    }

    /**
     * 返回处理线程
     */
    public class Writer implements Runnable {
        private MQMessage recvMsg;
        private int line;

        public Writer() {
        }

        public Writer(MQMessage recvMsg, int line) {
            this.recvMsg = recvMsg;
            this.line = line;
        }

        @Override
        public void run() {
            try {
                write(buildMQMessage(recvMsg), line);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private int end() throws Throwable {
        try {
            sendQueue.close();
            sendQueue1.close();
            recvQueue.close();
            recvQueue1.close();
            recvManager.disconnect();
            recvManager.close();
            recvManager1.disconnect();
            recvManager1.close();
            sendManager.disconnect();
            sendManager.close();
            sendManager1.disconnect();
            sendManager1.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public boolean isAlive() {
        return alive;
    }

    public void setAlive(boolean alive) {
        this.alive = alive;
    }

    public boolean isRecoveryFlag() {
        return recoveryFlag;
    }

    public void setRecoveryFlag(boolean recoveryFlag) {
        this.recoveryFlag = recoveryFlag;
    }


//    public static void main(String[] args) throws MQException, IOException {
//         MQQueueManager recvManager = null;
//         MQQueueManager sendManager = null;
//         MQQueue sendQueue = null;
//          MQQueue recvQueue = null;
//        Hashtable props = new Hashtable();
//        props.put(MQConstants.HOST_NAME_PROPERTY, "66.5.60.133");
//        props.put(MQConstants.PORT_PROPERTY, 10455);
//        props.put(MQConstants.CCSID_PROPERTY, new Integer(1208));
//        props.put(MQConstants.CHANNEL_PROPERTY, "SVRCONN_GW_ZHSD");
//        props.put(MQConstants.TRANSPORT_PROPERTY, MQConstants.TRANSPORT_MQSERIES);
//
//        if (recvManager == null) {
//            recvManager = new MQQueueManager("FT_QMG2GW02", props);
//        }
//        if (sendManager == null) {
//            sendManager = new MQQueueManager("FT_QMG2GW02", props);
//        }
//        int recvOpenOptions = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_FAIL_IF_QUIESCING;
//        if (recvQueue == null) {
//            recvQueue = recvManager.accessQueue("IBM.SERVICE.REQUEST.OUT.ZHSD", recvOpenOptions, null, null, null);
//        }
//        int sendOpenOptions = MQConstants.MQOO_OUTPUT | MQConstants.MQOO_FAIL_IF_QUIESCING;
//        if (sendQueue == null) {
//            sendQueue = sendManager.accessQueue("IBM.SERVICE.RESPONSE.IN", sendOpenOptions, null, null, null);
//        }
//
//        MQMessage recvMsg = new MQMessage();
//        MQGetMessageOptions gmo = new MQGetMessageOptions();
//        gmo.options = gmo.options | MQConstants.MQGMO_WAIT;
//        gmo.waitInterval = MQConstants.MQWI_UNLIMITED;
//        gmo.options = gmo.options + MQConstants.MQGMO_FAIL_IF_QUIESCING;
//        try {
//            if(true) {
//                recvQueue.get(recvMsg, gmo);
//            } else {
//                recvQueue1.get(recvMsg, gmo);
//            }
//
//        } catch(Exception ex) {
//            try {
//            } catch (Throwable e) {
//                e.printStackTrace();
//            }
//        }
//
//        if (recvMsg != null) {
//            log.info("MessageLength = " + recvMsg.getMessageLength());
//        }
//        byte[] recv = new byte[recvMsg.getMessageLength()];
//        recvMsg.readFully(recv);
//        String requestData = new String(recv, "UTF-8");
//        log.info("接受的信息是:" + requestData);
//        MQMessage sendMsg = new MQMessage();
//        MQPutMessageOptions pmo=new MQPutMessageOptions();
//        pmo.options |=8192;
//        sendMsg.expiry = 120000;
//        sendMsg.format = MQConstants.MQFMT_STRING;
//        sendMsg.messageId = recvMsg.messageId;
//        sendMsg.characterSet = 1208;
//        String response = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n"+
////                "<Service><Service_Header> \n"+
////        "	<reply_qmgr>FT_QMG2GW02</reply_qmgr>\n"+
//////                "	<processes>\n"+
//////                "		<process>\n"+
//////                "			<end_timestamp></end_timestamp>\n"+
//////                "			<process_timestamp>2021-04-15 17:16:40.136</process_timestamp>\n"+
//////                "			<resp_target_q>IBM.SERVICE.RESPONSE.OUT.ZHSD</resp_target_q>\n"+
//////                "			<req_target_q>IBM.SERVICE.REQUEST.OUT.ZHSD</req_target_q>\n"+
//////                "			<sub_target_id/>\n"+
//////                "			<target_id>2060</target_id>\n"+
//////                "			<sub_service_sn>9914561122029466699</sub_service_sn>\n"+
//////                "			<sub_reversal_service_id></sub_reversal_service_id>\n"+
//////                "			<async_reversal_service_id></async_reversal_service_id>\n"+
//////                "			<is_end>0</is_end>\n"+
//////                "			<skip_to_process>0</skip_to_process>\n"+
//////                "			<after_logic_class></after_logic_class>\n"+
//////                "			<timeout>150</timeout>\n"+
//////                "			<reversal_seq>0</reversal_seq>\n"+
//////                "			<key_service>0</key_service>\n"+
//////                "			<status>FAIL</status>\n"+
//////                "			<service_id>20600000000005</service_id>\n"+
//////                "<code>4002</code>\n"+
//////                "<desc></desc>\n"+
//////        "		</process>\n"+
//////                "	</processes>\n"+
//////                "	<service_response></service_response>\n"+
////                "	<msglog>1</msglog>\n"+
////                "	<timeout>150</timeout>\n"+
////                "	<name>日统计报表</name>\n"+
////                "	<resp_target_q>IBM.SERVICE.RESPONSE.OUT.ZHSD</resp_target_q>\n"+
////                "	<req_target_q>IBM.SERVICE.REQUEST.OUT.ZHSD</req_target_q>\n"+
////                "	<msg_expiry>360000</msg_expiry>\n"+
////                "	<start_time>1618478200132</start_time>\n"+
////                "	<start_timestamp>2021-04-15 17:16:40.131</start_timestamp>\n"+
////                "	<service_sn>9914561122029466701</service_sn>\n"+
////                "	<service_id>20600000000005</service_id>\n"+
////                "	<requester_id>2060</requester_id>\n"+
//////                "	<branch_id>320223001</branch_id>\n"+
//////                "	<channel_id>03</channel_id>\n"+
//////                "	<version_id>01</version_id>\n"+
//////                "	<service_time>20210412145611</service_time>\n"+
//////                "	<trace_msg>SERVICE.SINGLE.ROUTE - Sent single request to provider</trace_msg>\n"+
////                "</Service_Header>\n"+
////                "<Service_Body>\n"+
//////                "	<request>\n"+
//////                "		<requestCode>0521</requestCode>\n"+
//////                "		<merchantmchid>3201889991A0010001</merchantmchid>\n"+
//////                "		<begindate>20210411</begindate>\n"+
//////                "		<enddate>20210412</enddate>\n"+
//////                "		<mchtype>B</mchtype>\n"+
//////                "		<transstatus/>\n"+
//////                "		<transtype/>\n"+
//////                "		<tellerid/>\n"+
//////                "		<deviceinfo/>\n"+
//////                "	</request>\n"+
////                "	<response>\n"+
////                "		<status></status>\n"+
////                "	</response>\n"+
////                "	<MAC/>\n"+
////                "</Service_Body></Service>\n";
//                "<Service>\n                                              "+
//                "	<Service_Header>\n                                    "+
////                "<service_response></service_response>\n"+
////                "	<msglog>1</msglog>\n"+
////                "	<timeout>150</timeout>\n"+
//                "   <reply_qmgr>FT_QMG2GW02</reply_qmgr> \n"+
//                "	<resp_target_q>IBM.SERVICE.RESPONSE.OUT.ZHSD</resp_target_q>\n"+
//                "	<req_target_q>IBM.SERVICE.REQUEST.OUT.ZHSD</req_target_q>\n"+
////                "	<msg_expiry>360000</msg_expiry>\n"+
////                "	<start_time>1618478200132</start_time>\n"+
////                "	<start_timestamp>2021-04-15 17:16:40.131</start_timestamp>\n"+
//                "		<name>商户查询</name>\n                           "+
//                "		<service_sn>1740401005009522256</service_sn>\n    "+
//                "		<service_id>20600000000005</service_id>\n         "+
//                "		<requester_id>2060</requester_id>\n               "+
//                "		<branch_id>321105010</branch_id>\n                "+
//                "		<channel_id>01</channel_id>\n                     "+
//                "		<version_id>01</version_id>\n                     "+
//                "		<need_request>true</need_request>\n               "+
//                "		<PutTime>1614046675</PutTime>\n                   "+
//                "	</Service_Header>\n                                   "+
//                "	<Service_Body>\n                                      "+
//                "		<ext_attributes>\n                                "+
//                "			<channelcode>004</channelcode>\n              "+
//                "			<channeldate>20210223</channeldate>\n         "+
//                "			<channelseq>000522255</channelseq>\n          "+
//                "			<brno>321105010</brno>\n                      "+
//                "			<templatecode>500000</templatecode>\n            "+
//                "			<transcode>200002</transcode>\n            "+
//                "			<sysid>000165</sysid>\n            "+
//                "			<terminal_unitno>924301070110025</terminal_unitno>\n        "+
//                "			<terminal_name>扬中农村商户20180808</terminal_name>\n       "+
//                "			<terminalno>40401005</terminalno>\n            "+
//                "			<terminal_acctno>10132110501222412505800001</terminal_acctno>\n     "+
//                "			<INM-ENC-DEV-ID>pos.40401005.zpk</INM-ENC-DEV-ID>\n        "+
//                "			<INM-PAGE-UP-DOWN>2</INM-PAGE-UP-DOWN>\n          "+
//                "			<plat_version>two_plat</plat_version>\n        "+
//                "		</ext_attributes>\n        "+
//                "		<response>\n            "+
//                "			<dataCode>00</dataCode>\n            "+
//                "			<dataMessage>查询成功</dataMessage>\n    "+
//                "			<data>\n                "+
//                "				<total>0</total>\n      "+
//                "				<rows></rows>\n       "+
//                "			</data>\n        "+
//                "		</response>\n       "+
//                "		<MAC>None</MAC>\n   "+
//                "	</Service_Body> \n"+
//                "</Service>\n";
//        byte[] content = response.getBytes("UTF-8");
//        sendMsg.write(content);
//        sendQueue.put(sendMsg,pmo);
//        sendManager.commit();
//        log.info("返回的信息是:" + response);
//    }

    public static void main(String[] args) throws IOException, MQException {
        MqService mqService = new MqService();
        String response = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n" +
//                "<Service><Service_Header> \n"+
//        "	<reply_qmgr>FT_QMG2GW02</reply_qmgr>\n"+
////                "	<processes>\n"+
////                "		<process>\n"+
////                "			<end_timestamp></end_timestamp>\n"+
////                "			<process_timestamp>2021-04-15 17:16:40.136</process_timestamp>\n"+
////                "			<resp_target_q>IBM.SERVICE.RESPONSE.OUT.ZHSD</resp_target_q>\n"+
////                "			<req_target_q>IBM.SERVICE.REQUEST.OUT.ZHSD</req_target_q>\n"+
////                "			<sub_target_id/>\n"+
////                "			<target_id>2060</target_id>\n"+
////                "			<sub_service_sn>9914561122029466699</sub_service_sn>\n"+
////                "			<sub_reversal_service_id></sub_reversal_service_id>\n"+
////                "			<async_reversal_service_id></async_reversal_service_id>\n"+
////                "			<is_end>0</is_end>\n"+
////                "			<skip_to_process>0</skip_to_process>\n"+
////                "			<after_logic_class></after_logic_class>\n"+
////                "			<timeout>150</timeout>\n"+
////                "			<reversal_seq>0</reversal_seq>\n"+
////                "			<key_service>0</key_service>\n"+
////                "			<status>FAIL</status>\n"+
////                "			<service_id>20600000000005</service_id>\n"+
////                "<code>4002</code>\n"+
////                "<desc></desc>\n"+
////        "		</process>\n"+
////                "	</processes>\n"+
////                "	<service_response></service_response>\n"+
//                "	<msglog>1</msglog>\n"+
//                "	<timeout>150</timeout>\n"+
//                "	<name>日统计报表</name>\n"+
//                "	<resp_target_q>IBM.SERVICE.RESPONSE.OUT.ZHSD</resp_target_q>\n"+
//                "	<req_target_q>IBM.SERVICE.REQUEST.OUT.ZHSD</req_target_q>\n"+
//                "	<msg_expiry>360000</msg_expiry>\n"+
//                "	<start_time>1618478200132</start_time>\n"+
//                "	<start_timestamp>2021-04-15 17:16:40.131</start_timestamp>\n"+
//                "	<service_sn>9914561122029466701</service_sn>\n"+
//                "	<service_id>20600000000005</service_id>\n"+
//                "	<requester_id>2060</requester_id>\n"+
////                "	<branch_id>320223001</branch_id>\n"+
////                "	<channel_id>03</channel_id>\n"+
////                "	<version_id>01</version_id>\n"+
////                "	<service_time>20210412145611</service_time>\n"+
////                "	<trace_msg>SERVICE.SINGLE.ROUTE - Sent single request to provider</trace_msg>\n"+
//                "</Service_Header>\n"+
//                "<Service_Body>\n"+
////                "	<request>\n"+
////                "		<requestCode>0521</requestCode>\n"+
////                "		<merchantmchid>3201889991A0010001</merchantmchid>\n"+
////                "		<begindate>20210411</begindate>\n"+
////                "		<enddate>20210412</enddate>\n"+
////                "		<mchtype>B</mchtype>\n"+
////                "		<transstatus/>\n"+
////                "		<transtype/>\n"+
////                "		<tellerid/>\n"+
////                "		<deviceinfo/>\n"+
////                "	</request>\n"+
//                "	<response>\n"+
//                "		<status></status>\n"+
//                "	</response>\n"+
//                "	<MAC/>\n"+
//                "</Service_Body></Service>\n";
                "<Service>\n                                              " +
                "	<Service_Header>\n                                    " +
//                "<service_response></service_response>\n"+
//                "	<msglog>1</msglog>\n"+
//                "	<timeout>150</timeout>\n"+
                "   <reply_qmgr>FT_QMG2GW02</reply_qmgr> \n" +
                "	<resp_target_q>IBM.SERVICE.RESPONSE.OUT.ZHSD</resp_target_q>\n" +
                "	<req_target_q>IBM.SERVICE.REQUEST.OUT.ZHSD</req_target_q>\n" +
//                "	<msg_expiry>360000</msg_expiry>\n"+
//                "	<start_time>1618478200132</start_time>\n"+
//                "	<start_timestamp>2021-04-15 17:16:40.131</start_timestamp>\n"+
                "		<name>商户查询</name>\n                           " +
                "		<service_sn>1740401005009522268</service_sn>\n    " +
                "		<service_id>20600000000005</service_id>\n         " +
                "		<requester_id>2060</requester_id>\n               " +
                "		<branch_id>321105010</branch_id>\n                " +
                "		<channel_id>01</channel_id>\n                     " +
                "		<version_id>01</version_id>\n                     " +
                "		<need_request>true</need_request>\n               " +
                "		<PutTime>1614046675</PutTime>\n                   " +
                "	</Service_Header>\n                                   " +
                "	<Service_Body>\n                                      " +
                "		<ext_attributes>\n                                " +
                "			<channelcode>004</channelcode>\n              " +
                "			<channeldate>20210223</channeldate>\n         " +
                "			<channelseq>000522255</channelseq>\n          " +
                "			<brno>321105010</brno>\n                      " +
                "			<templatecode>500000</templatecode>\n            " +
                "			<transcode>200002</transcode>\n            " +
                "			<sysid>000165</sysid>\n            " +
                "			<terminal_unitno>924301070110025</terminal_unitno>\n        " +
                "			<terminal_name>扬中农村商户20180808</terminal_name>\n       " +
                "			<terminalno>40401005</terminalno>\n            " +
                "			<terminal_acctno>10132110501222412505800001</terminal_acctno>\n     " +
                "			<INM-ENC-DEV-ID>pos.40401005.zpk</INM-ENC-DEV-ID>\n        " +
                "			<INM-PAGE-UP-DOWN>2</INM-PAGE-UP-DOWN>\n          " +
                "			<plat_version>two_plat</plat_version>\n        " +
                "		</ext_attributes>\n        " +
                "		<response>\n            " +
                "			<dataCode>00</dataCode>\n            " +
                "			<dataMessage>查询成功</dataMessage>\n    " +
                "			<transInfos>\n    " +
                "<transInfo>                                              \n" +
                "	<outTradeNo>2021000001</outTradeNo>                   \n" +
                "	<transType>1</transType>                              \n" +
                "	<transStatus>0</transStatus>                          \n" +
                "	<transAmount>39</transAmount>                         \n" +
                "	<transDatetime>2021-04-16 16:59:59</transDatetime>    \n" +
                "	<remark>微信交易</remark>                             \n" +
                "	<handlingCharge>7.8</handlingCharge>                  \n" +
                "	<shopName>江苏农商银行</shopName>                     \n" +
                "	<mchntName>信雅达</mchntName>                         \n" +
                "	</transInfo>                                          \n" +

                "<transInfo>                                              \n" +
                "	<outTradeNo>2021000002</outTradeNo>                   \n" +
                "	<transType>1</transType>                              \n" +
                "	<transStatus>0</transStatus>                          \n" +
                "	<transAmount>39</transAmount>                         \n" +
                "	<transDatetime>2021-04-16 16:59:59</transDatetime>    \n" +
                "	<remark>微信交易</remark>                             \n" +
                "	<handlingCharge>7.8</handlingCharge>                  \n" +
                "	<shopName>江苏农商银行</shopName>                     \n" +
                "	<mchntName>信雅达</mchntName>                         \n" +
                "	</transInfo>                                          \n" +

                "<transInfo>                                              \n" +
                "	<outTradeNo>2021000003</outTradeNo>                   \n" +
                "	<transType>1</transType>                              \n" +
                "	<transStatus>0</transStatus>                          \n" +
                "	<transAmount>39</transAmount>                         \n" +
                "	<transDatetime>2021-04-16 16:59:59</transDatetime>    \n" +
                "	<remark>微信交易</remark>                             \n" +
                "	<handlingCharge>7.8</handlingCharge>                  \n" +
                "	<shopName>江苏农商银行</shopName>                     \n" +
                "	<mchntName>信雅达</mchntName>                         \n" +
                "	</transInfo>                                          \n" +

                "<transInfo>                                              \n" +
                "	<outTradeNo>2021000004</outTradeNo>                   \n" +
                "	<transType>1</transType>                              \n" +
                "	<transStatus>0</transStatus>                          \n" +
                "	<transAmount>39</transAmount>                         \n" +
                "	<transDatetime>2021-04-16 16:59:59</transDatetime>    \n" +
                "	<remark>微信交易</remark>                             \n" +
                "	<handlingCharge>7.8</handlingCharge>                  \n" +
                "	<shopName>江苏农商银行</shopName>                     \n" +
                "	<mchntName>信雅达</mchntName>                         \n" +
                "	</transInfo>                                          \n" +

                "<transInfo>                                              \n" +
                "	<outTradeNo>2021000005</outTradeNo>                   \n" +
                "	<transType>1</transType>                              \n" +
                "	<transStatus>0</transStatus>                          \n" +
                "	<transAmount>39</transAmount>                         \n" +
                "	<transDatetime>2021-04-16 16:59:59</transDatetime>    \n" +
                "	<remark>微信交易</remark>                             \n" +
                "	<handlingCharge>7.8</handlingCharge>                  \n" +
                "	<shopName>江苏农商银行</shopName>                     \n" +
                "	<mchntName>信雅达</mchntName>                         \n" +
                "	</transInfo>                                          \n" +

                "<transInfo>                                              \n" +
                "	<outTradeNo>2021000006</outTradeNo>                   \n" +
                "	<transType>1</transType>                              \n" +
                "	<transStatus>0</transStatus>                          \n" +
                "	<transAmount>39</transAmount>                         \n" +
                "	<transDatetime>2021-04-16 16:59:59</transDatetime>    \n" +
                "	<remark>微信交易</remark>                             \n" +
                "	<handlingCharge>7.8</handlingCharge>                  \n" +
                "	<shopName>江苏农商银行</shopName>                     \n" +
                "	<mchntName>信雅达</mchntName>                         \n" +
                "	</transInfo>                                          \n" +

                "<transInfo>                                              \n" +
                "	<outTradeNo>2021000007</outTradeNo>                   \n" +
                "	<transType>1</transType>                              \n" +
                "	<transStatus>0</transStatus>                          \n" +
                "	<transAmount>39</transAmount>                         \n" +
                "	<transDatetime>2021-04-16 16:59:59</transDatetime>    \n" +
                "	<remark>微信交易</remark>                             \n" +
                "	<handlingCharge>7.8</handlingCharge>                  \n" +
                "	<shopName>江苏农商银行</shopName>                     \n" +
                "	<mchntName>信雅达</mchntName>                         \n" +
                "	</transInfo>                                          \n" +

                "<transInfo>                                              \n" +
                "	<outTradeNo>2021000008</outTradeNo>                   \n" +
                "	<transType>1</transType>                              \n" +
                "	<transStatus>0</transStatus>                          \n" +
                "	<transAmount>39</transAmount>                         \n" +
                "	<transDatetime>2021-04-16 16:59:59</transDatetime>    \n" +
                "	<remark>微信交易</remark>                             \n" +
                "	<handlingCharge>7.8</handlingCharge>                  \n" +
                "	<shopName>江苏农商银行</shopName>                     \n" +
                "	<mchntName>信雅达</mchntName>                         \n" +
                "	</transInfo>                                          \n" +

                "<transInfo>                                              \n" +
                "	<outTradeNo>202100009</outTradeNo>                   \n" +
                "	<transType>1</transType>                              \n" +
                "	<transStatus>0</transStatus>                          \n" +
                "	<transAmount>39</transAmount>                         \n" +
                "	<transDatetime>2021-04-16 16:59:59</transDatetime>    \n" +
                "	<remark>微信交易</remark>                             \n" +
                "	<handlingCharge>7.8</handlingCharge>                  \n" +
                "	<shopName>江苏农商银行</shopName>                     \n" +
                "	<mchntName>信雅达</mchntName>                         \n" +
                "	</transInfo>                                          \n" +

                "<transInfo>                                              \n" +
                "	<outTradeNo>2021000010</outTradeNo>                   \n" +
                "	<transType>1</transType>                              \n" +
                "	<transStatus>0</transStatus>                          \n" +
                "	<transAmount>39</transAmount>                         \n" +
                "	<transDatetime>2021-04-16 16:59:59</transDatetime>    \n" +
                "	<remark>微信交易</remark>                             \n" +
                "	<handlingCharge>7.8</handlingCharge>                  \n" +
                "	<shopName>江苏农商银行</shopName>                     \n" +
                "	<mchntName>信雅达</mchntName>                         \n" +
                "	</transInfo>                                          \n" +
                "			</transInfos>                                 \n" +
                "		</response>\n       " +
                "		<MAC>None</MAC>\n   " +
                "	</Service_Body> \n" +
                "</Service>\n";
        MQMessage recvMsg = mqService.serviceRec();
        mqService.serviceSend(recvMsg, response);

    }

    public void exec() throws IOException {
        CountDownLatch countDownLatch = new CountDownLatch(3);
        for (int i = 0; i < 40; i++) {
            Thread thread = new Thread(new MqService());
            thread.setName("线程-" + (i + 1));
            thread.start();
            countDownLatch.countDown();
        }
    }

    public MQMessage serviceRec() throws IOException, MQException {
        MQQueueManager queueManager = null;
        MQQueue recvQueue = null;
        Hashtable props = new Hashtable();
        props.put(MQConstants.HOST_NAME_PROPERTY, "66.5.60.133");
        props.put(MQConstants.PORT_PROPERTY, 10455);
        props.put(MQConstants.CCSID_PROPERTY, new Integer(1381));
        props.put(MQConstants.CHANNEL_PROPERTY, "SVRCONN_GW_ZHSD");
        props.put(MQConstants.TRANSPORT_PROPERTY, MQConstants.TRANSPORT_MQSERIES);

        if (queueManager == null) {
            queueManager = new MQQueueManager("FT_QMG2GW02", props);
        }
        int recvOpenOptions = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_FAIL_IF_QUIESCING;
        if (recvQueue == null) {
            recvQueue = queueManager.accessQueue("IBM.SERVICE.REQUEST.OUT.ZHSD", recvOpenOptions, null, null, null);
        }
        MQMessage recvMsg = new MQMessage();
        MQGetMessageOptions gmo = new MQGetMessageOptions();
        gmo.options = gmo.options | MQConstants.MQGMO_WAIT;
        gmo.waitInterval = MQConstants.MQWI_UNLIMITED;
        gmo.options = gmo.options + MQConstants.MQGMO_FAIL_IF_QUIESCING;
        try {
            if (true) {
                recvQueue.get(recvMsg, gmo);
            } else {
                recvQueue1.get(recvMsg, gmo);
            }
            byte[] recv = new byte[recvMsg.getMessageLength()];
            recvMsg.readFully(recv);
            String requestData = new String(recv, "UTF-8");
            log.info("请求信息:" + requestData);
        } catch (Exception ex) {
            try {
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        return recvMsg;
    }

    public void serviceSend(MQMessage recvMsg, String respXml) throws IOException, MQException {
        MQQueueManager queueManager = null;
        MQQueue sendQueue = null;
        Hashtable props = new Hashtable();
        props.put(MQConstants.HOST_NAME_PROPERTY, "66.5.60.133");
        props.put(MQConstants.PORT_PROPERTY, 10455);
        props.put(MQConstants.CCSID_PROPERTY, new Integer(1381));
        props.put(MQConstants.CHANNEL_PROPERTY, "SVRCONN_GW_ZHSD");
        props.put(MQConstants.TRANSPORT_PROPERTY, MQConstants.TRANSPORT_MQSERIES);
        if (queueManager == null) {
            queueManager = new MQQueueManager("FT_QMG2GW02", props);
        }
        int sendOpenOptions = MQConstants.MQOO_OUTPUT | MQConstants.MQOO_FAIL_IF_QUIESCING;
        if (sendQueue == null) {
            sendQueue = queueManager.accessQueue("IBM.SERVICE.RESPONSE.IN", sendOpenOptions, null, null, null);
        }
        MQMessage sendMsg = new MQMessage();
        MQPutMessageOptions pmo = new MQPutMessageOptions();
        pmo.options |= 8192;
        sendMsg.expiry = 120000;
        sendMsg.format = MQConstants.MQFMT_STRING;
        sendMsg.messageId = recvMsg.messageId;
        sendMsg.characterSet = 1208;
        byte[] content = respXml.getBytes("UTF-8");
        sendMsg.write(content);
        sendQueue.put(sendMsg, pmo);
        queueManager.commit();
        log.info("响应信息:" + respXml);
    }
}

